package com.freeter.modules.user.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.freeter.common.mpextend.parser.ParseWrapper;
import com.freeter.common.utils.JQPageInfo;
import com.freeter.common.utils.PageInfo;
import com.freeter.common.utils.PageUtils;
import com.freeter.common.utils.R;
import com.freeter.common.validator.ValidatorUtils;
import com.freeter.modules.user.entity.UsersEntity;
import com.freeter.modules.user.entity.model.UsersModel;
import com.freeter.modules.user.entity.view.UsersView;
import com.freeter.modules.user.service.UserAuthsService;
import com.freeter.modules.user.service.UsersService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;


/**
 * 用户基础信息表
 * 后端接口
 *
 * @author xuchen
 * @email 171998110@qq.com
 * @date 2019-04-07 16:11:14
 */
@RestController
@RequestMapping("user/users")
@SuppressWarnings({"unchecked", "rawtypes"})
public class UsersController {
    @Autowired
    private UsersService usersService;
    @Autowired
    private UserAuthsService userAuthsService;

    /**
     * 列表
     */
    @RequestMapping("/page")
    @RequiresPermissions("user:users:list")
    public PageUtils page(JQPageInfo jqPageInfo, UsersModel users) {
        EntityWrapper<UsersEntity> ew = ParseWrapper.parseWrapper(users);
        PageInfo pageInfo = new PageInfo(jqPageInfo);
        PageUtils page = usersService.queryPage(pageInfo, ew);

        return page;

    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("user:users:list")
    public R list(UsersModel users) {
        EntityWrapper<UsersEntity> ew = ParseWrapper.parseWrapper(users);
        return R.ok().put("data", usersService.selectListView(ew));
    }

    /**
     * 查询
     */
    @RequestMapping("/query")
    @RequiresPermissions("user:users:info")
    public R query(UsersEntity users) {
        EntityWrapper<UsersEntity> ew = ParseWrapper.parseWrapper(users);
        UsersView usersView = usersService.selectView(ew);
        return R.ok("查询用户基础信息表成功").put("data", usersView);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{userId}")
    @RequiresPermissions("user:users:info")
    public R info(@PathVariable("userId") String userId) {
        UsersEntity users = usersService.selectById(userId);

        return R.ok().put("users", users);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("user:users:save")
    public R save(@RequestBody UsersEntity users) {
        ValidatorUtils.validateEntity(users);
        usersService.insert(users);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("user:users:update")
    public R update(@RequestBody UsersEntity users) {
        ValidatorUtils.validateEntity(users);
        usersService.updateAllColumnById(users);//全部更新

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("user:users:delete")
    public R delete(@RequestBody String[] userIds) {
        usersService.deleteBatchIds(Arrays.asList(userIds));
        //授权表也删除
        userAuthsService.deleteBatchIds(Arrays.asList(userIds));
        return R.ok();
    }

}
